package com.qk.controller;

import com.qk.common.Result;
import com.qk.utils.AliyunOSSOperator;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.util.UUID;

@Slf4j
@RestController
public class UploadController {

    @Autowired
    private AliyunOSSOperator aliyunOSSOperator;

    /*@PostMapping("/upload")
    public Result upload(String username, Integer age, MultipartFile file) throws Exception {
        log.info("上传文件: {}, {}, {}",username, age, file);
        //获取原始文件名
        String  fileName = file.getOriginalFilename();
        //截取文件名拓展名
        String fileType = fileName.substring(fileName.lastIndexOf("."));
        //拼接新的文件名
        String newFileName = UUID.randomUUID().toString() + fileType;
        //保存文件到指定路径
        file.transferTo(new File("D:\\Users\\王世君\\Desktop\\Git-Test01\\" + newFileName));
        return Result.success();
    }*/
    @PostMapping("/upload")
    //接收文件
    public Result upload(MultipartFile image) throws Exception {
        log.info("上传文件: {}", image);
        //获取原始文件名
        String  fileName = image.getOriginalFilename();
        //截取文件名拓展名
        String fileType = fileName.substring(fileName.lastIndexOf("."));
        //拼接新的文件名
        String newFileName = UUID.randomUUID().toString() + fileType;

        //调用阿里云上传方法
        String url = aliyunOSSOperator.upload(image.getBytes(), newFileName);
        return Result.success(url);
    }
}
